//
//  AiVoiceChooseView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/1/16.
//

import SwiftUI

//语音选择试图
struct AiVoiceChooseView: View {
    @StateObject var systemVoiceViewModel = SystemVoiceViewModel()
    @StateObject var myVoiceViewModel = MyVoiceViewModel()
    @StateObject var makeVoiceViewModel = MakeVoiceViewModel()
    @StateObject var makeVoiceDoneViewModel = MakeVoiceDoneViewModel()
    var choose = false
    let onChoose: (AiVoiceBean) -> Void

    @State private var selectedOption = "系统音色"
    let options = ["系统音色","我的音色","制作音色"]
    let options2 = ["制作音色"]
    var body: some View {
        VStack {
            if !isExamine() {
                Picker("系统音色", selection: $selectedOption) {
                    ForEach(isExamine() ? options2 : options,id : \.self){ option in
                        Text(option)
                    }
                }.pickerStyle(.segmented)
                    .padding(EdgeInsets(top: 30, leading: 20, bottom: 10, trailing: 20))
            }
            // Spacer 用于占据剩余空间
            Spacer()
            Group {
                if selectedOption == "系统音色" {
                    SystemVoiceView(viewModel: systemVoiceViewModel, choose: choose) { aiVoiceBean in
                        onChoose(aiVoiceBean)
                    }.frame(maxHeight: .infinity)
                } else if selectedOption == "我的音色" {
                    MyVoiceView(viewModel: myVoiceViewModel, choose: choose) { aiVoiceBean in
                        onChoose(aiVoiceBean)
                    }.frame(maxHeight: .infinity)
                } else if selectedOption == "制作音色" {
                    CustomVoiceView(viewModel:makeVoiceViewModel,viewModelDone:makeVoiceDoneViewModel).frame(maxHeight: .infinity)
                }
            }
        }
        .onAppear {
            if isExamine() {
                selectedOption = "制作音色"
            }
        }
        .onDisappear{
            systemVoiceViewModel.cancelRequest()
            myVoiceViewModel.cancelRequest()
            makeVoiceViewModel.cancelRequest()
            makeVoiceDoneViewModel.cancelRequest()
        }
        
    }
}

#Preview {
    AiVoiceChooseView(){_ in}
}
